package com.zenika.zenbinding.client.renderer;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.zenika.zenbinding.client.ZenBinder;
import com.zenika.zenbinding.client.feedback.DummyFeedbackManager;
import com.zenika.zenbinding.client.feedback.FeedbackManager;
import com.zenika.zenbinding.client.model.BusinessObject;
import com.zenika.zenbinding.client.model.ValueModel;
import com.zenika.zenbinding.client.validator.Validator;

public class TextBoxRenderer implements Renderer {

	private String property;
	private String style;
	private List validators;
	private FeedbackManager manager;

	public TextBoxRenderer(String property) {
		this.property = property;
		this.validators = new ArrayList();
	}

	public Widget render(BusinessObject object) {
		TextBox textBox = new TextBox();
		if (this.style != null) {
			textBox.setStyleName(this.style);
		}
		ValueModel model = ZenBinder.bindValue(textBox, object, this.property);
		Iterator iter = this.validators.iterator();
		while (iter.hasNext()) {
			model.addValidator((Validator) iter.next());
		}
		if (this.manager != null) {
			model.setFeedbackManager(this.manager);
		}
		return textBox;
	}

	public Widget render(BusinessObject object, DummyFeedbackManager manager) {
		TextBox textBox = new TextBox();
		if (this.style != null) {
			textBox.setStyleName(this.style);
		}
		ValueModel model = ZenBinder.bindValue(textBox, object, this.property);
		Iterator iter = this.validators.iterator();
		while (iter.hasNext()) {
			model.addValidator((Validator) iter.next());
		}
		if (this.manager != null) {
			manager.setFeedbackManager(this.manager);
		}
		model.setFeedbackManager(manager);
		return textBox;
	}

	public void setStyleName(String style) {
		this.style = style;
	}

	public void addValidator(Validator validator) {
		this.validators.add(validator);
	}

	public void setFeedbackManager(FeedbackManager manager) {
		this.manager = manager;
	}

}
